//package com.tsk.example.config.datasource;
//
//import cn.com.gf.cap.bootstrap.log.mybatis.SqlLogIntercept;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.mybatis.spring.annotation.MapperScan;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//
//import javax.annotation.Resource;
//import javax.sql.DataSource;
//
///**
// * @author: 黄嘉振
// * @create: 2023/2/20 13:21
// * @description:    不同的数据源使用不同的目录，cap为中台数据源，其他的数据源名称与目录保持一致即可
// **/
//@Slf4j
//@Configuration
//@MapperScan(basePackages = "{cn.com.gf.cap.demo.mapper.cap}", sqlSessionFactoryRef = "capSqlSessionFactory")
//public class CapDbConfig {
//    private static final String MAPPER_LOCATIONS = "classpath:mapper/cap/*.xml";
//
//    private static final String LOG_PREFIX = "dao.";
//
//    @Value("${spring.datasource.name}")
//    private String name;
//
//    @Resource
//    private SqlLogIntercept logIntercept;
//
//    @Bean(name = "capDataSource")
//    @ConfigurationProperties("spring.datasource")
//    public DataSource dataSource(DataSourceProperties properties) {
//        return properties.initializeDataSourceBuilder()
//                .build();
//    }
//
//    @Primary
//    @Bean(name = "capSqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("capDataSource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dataSource);
//        log.debug("数据库连接：{}", name);
//        bean.setPlugins(new Interceptor[] { logIntercept });
//        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//        configuration.setLogPrefix(LOG_PREFIX);
//        configuration.setMapUnderscoreToCamelCase(true);
//        bean.setConfiguration(configuration);
//        bean.setMapperLocations(
//                new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATIONS));
//        return bean.getObject();
//    }
//
//    @Bean(name = "capTransactionManager")
//    @Primary
//    public DataSourceTransactionManager transactionManager(@Qualifier("capDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }
//
//    @Bean(name = "capSqlSessionTemplate")
//    @Primary
//    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("capSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
//}
